package com.gmss.crawler.service;

import com.alibaba.fastjson.JSON;
import com.gmss.crawler.enums.AcademicianEnum;
import com.gmss.crawler.module.person.domain.PersonInfo;
import com.gmss.crawler.utils.CrawlerCommonUtils;
import com.gmss.crawler.utils.DateUtils;
import com.gmss.crawler.utils.RegexUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 长江学者
 */
@Slf4j
@Service
public class ScholarService {

    public static final String url = "https://chinois.jinzhao.wiki";

    public static final Map<String, String> map = new HashMap() {{
        put("第一批", "1999");
        put("第二批", "2000");
        put("第三批", "2001");
        put("第四批", "2002");
        put("第五批", "2003");
        put("第六批", "2004");
        put("第七批", "2005");
        put("第八批", "2006");
        put("2007年度", "2007");
        put("2008年度", "2008");
        put("2009年度", "2009");
        put("2011年度", "2011");
        put("2012年度", "2012");
        put("2013年、2014年度", "2013、2014");
        put("2013、2014年度", "2013、2014");
        put("2015年度", "2015");
        put("2016年度", "2016");
        put("2017年度", "2017");
    }};


    public static void main(String[] args) throws IOException {
//        System.out.println(JSON.toJSONString(new ScholarService().crawlerScholar()));
        System.out.println(JSON.toJSONString(new ScholarService().parseData()));
        List<PersonInfo> list = new ArrayList<>();
//        updateScholar2011(list, "2011");
//        updateScholarByYear(list, "2005");
//        updateScholarByYear(list, "2006");
//        updateScholarByYear(list, "2007");
//        updateScholarByYear(list, "2008");
    }


    public List<PersonInfo> crawlerScholar() throws IOException {
        //Document doc = CrawlerCommonUtils.getDocument("https://chinois.jinzhao.wiki/wiki/%E9%95%BF%E6%B1%9F%E5%AD%A6%E8%80%85%E7%89%B9%E8%81%98%E6%95%99%E6%8E%88%E5%88%97%E8%A1%A8");
        Document doc = CrawlerCommonUtils.getDocumentHtml("/htmlTest/cj-长江学者/cj-长江学者-特聘教授.html");
        Elements tableElements = doc.getElementById("mw-content-text").select(".mw-parser-output table");
        Elements h2Elements = doc.getElementById("mw-content-text").select(".mw-parser-output h2");
        List<PersonInfo> list = new ArrayList<>();
        String personUrl = "";
        String substring = "";
        for (int i = 1; i < tableElements.size() - 2; i++) {
            Elements trElements = tableElements.get(i).select("tbody tr");
            //获取h2的内容并替换
            String text = h2Elements.get(i + 1).text().replaceAll("[编辑]", "[编辑]");
            //截取h2的内容成需要的字符串
            substring = text.substring(0, text.indexOf('['));
            for (int j = 1; j < trElements.size(); j++) {
                // TODO academicianTitle，electionYear，titleYears，workCompany，relatedLinks
                Elements tdElemnets = trElements.get(j).select("td");
                PersonInfo personInfo = new PersonInfo();
                //获取头衔
                personInfo.setAcademicianTitle(8);
                personInfo.setAcademicianTitleValue(AcademicianEnum.getEnum(8).getValue());
                //当选年
                personInfo.setElectionYear(map.get(substring));
                //获取年限
                personInfo.setTitleYears(5);
                //姓名
                String name = tdElemnets.get(0).select("a").text();
                String titleText = tdElemnets.get(0).select("a").attr("title");
                if (StringUtils.isBlank(name)) {
                    name = tdElemnets.get(0).text();
                }
                personInfo.setName(name);
                if (StringUtils.isNotBlank(tdElemnets.get(0).select("small").text())) {
                    personInfo.setForeignName(tdElemnets.get(0).select("small").text());
                }
                String str = tdElemnets.get(0).attr("bgcolor");
                if (StringUtils.containsAny(tdElemnets.get(0).attr("bgcolor"), "#FFFF99", "#CCFF99", "FFFF99", "CCFF99")) {
                    // 证明是院士，只存储姓名和标识即可
                    personInfo.setStamp(1);
                    list.add(personInfo);
                } else if (titleText.contains("不存在")) {
                    personInfo.setMajor(tdElemnets.get(1).text());
                    String company = tdElemnets.get(2).text().replaceAll("（", "(").replaceAll("）", ")");
                    //获取工作单位
                    personInfo.setWorkCompany(company);
                    list.add(personInfo);
                } else {
                    //学科
                    personInfo.setMajor(tdElemnets.get(1).text());
                    //聘任学校
                    String company = tdElemnets.get(2).text().replaceAll("（", "(").replaceAll("）", ")");
                    //获取工作单位
                    personInfo.setWorkCompany(company);
                    //个人详情地址
                    personUrl = url + tdElemnets.get(0).select("a").attr("href");
                    if (StringUtils.isNotBlank(tdElemnets.get(0).select("a").attr("href"))) {
                        Map<String, String> linksMap = new HashMap<>();
                        linksMap.put("relatedLinksName", "长江学者-特聘教授");
                        linksMap.put("relatedLinksUrl", personUrl);
                        //获取相关链接
                        personInfo.setRelatedLinks(JSON.toJSONString(linksMap));
                    }
                    //解析地址，获取信息
//                    parsePersonData(personUrl, personInfo);
                    list.add(personInfo);

                }
            }
        }
//        System.out.println(JSON.toJSONString(list));
        return list;
    }


    public List<PersonInfo> crawlerScholarJiangZuo() throws IOException {
        //Document doc = CrawlerCommonUtils.getDocument("https://chinois.jinzhao.wiki/wiki/%E9%95%BF%E6%B1%9F%E5%AD%A6%E8%80%85%E7%89%B9%E8%81%98%E6%95%99%E6%8E%88%E5%88%97%E8%A1%A8");
        Document doc = CrawlerCommonUtils.getDocumentHtml("/htmlTest/cj-长江学者/cj-长江学者-讲座教授.html");
        Elements tableElements = doc.getElementById("mw-content-text").select("table");
        Elements h2Elements = doc.getElementById("mw-content-text").select("h2");
        List<PersonInfo> list = new ArrayList<>();
        String personUrl = "";
        String substring = "";
        for (int i = 1; i < tableElements.size() - 1; i++) {
            Elements trElements = tableElements.get(i).select("tbody tr");
            //获取h2的内容并替换
            String text = h2Elements.get(i + 1).text().replaceAll("[编辑]", "[编辑]");
            //截取h2的内容成需要的字符串
            substring = text.substring(0, text.indexOf('['));
            for (int j = 1; j < trElements.size(); j++) {
                // TODO academicianTitle，electionYear，titleYears，workCompany，relatedLinks
                Elements tdElemnets = trElements.get(j).select("td");
                PersonInfo personInfo = new PersonInfo();
                //获取头衔
                personInfo.setAcademicianTitle(10);
                personInfo.setAcademicianTitleValue(AcademicianEnum.getEnum(10).getValue());
                //当选年
                personInfo.setElectionYear(map.get(substring));
                //获取年限
                personInfo.setTitleYears(3);
                //姓名
                String name = tdElemnets.get(0).select("a").text();
                String titleText = tdElemnets.get(0).select("a").attr("title");
                if (StringUtils.isBlank(name)) {
                    name = tdElemnets.get(0).text();
                }
                personInfo.setName(name);
                if (StringUtils.isNotBlank(tdElemnets.get(0).select("small").text())) {
                    personInfo.setForeignName(tdElemnets.get(0).select("small").text());
                }
                String str = tdElemnets.get(0).attr("bgcolor");
                if (StringUtils.containsAny(str, "#FFFF99", "#CCFF99", "FFFF99", "CCFF99")) {
                    // 证明是院士，只存储姓名和标识即可
                    personInfo.setStamp(1);
                    list.add(personInfo);
                } else if (titleText.contains("不存在")) {
                    if (StringUtils.isNotBlank(tdElemnets.get(1).text())) {
                        personInfo.setNationality(tdElemnets.get(1).text());
                    }
                    personInfo.setMajor(tdElemnets.get(2).text());
                    String company = tdElemnets.get(3).text();
                    //获取工作单位
                    personInfo.setWorkCompany(company.replaceAll("（", "(").replaceAll("）", ")"));
                    list.add(personInfo);
                } else {
                    if (StringUtils.isNotBlank(tdElemnets.get(1).text())) {
                        personInfo.setNationality(tdElemnets.get(1).text());
                    }
                    //学科
                    personInfo.setMajor(tdElemnets.get(2).text());
                    //聘任学校
                    String company = tdElemnets.get(3).text();
                    //获取工作单位
                    personInfo.setWorkCompany(company.replaceAll("（", "(").replaceAll("）", ")"));
                    //个人详情地址
                    personUrl = url + tdElemnets.get(0).select("a").attr("href");
                    if (StringUtils.isNotBlank(tdElemnets.get(0).select("a").attr("href"))) {
                        Map<String, String> linksMap = new HashMap<>();
                        linksMap.put("relatedLinksName", "长江学者-讲座教授");
                        linksMap.put("relatedLinksUrl", personUrl);
                        //获取相关链接
                        personInfo.setRelatedLinks(JSON.toJSONString(linksMap));
                    }
                    //解析地址，获取信息
//                    parsePersonData(personUrl, personInfo);
                    list.add(personInfo);

                }
            }
        }

        Elements trElements = tableElements.get(15).select("tbody tr");
        //获取h2的内容并替换
        String text = h2Elements.get(15 + 1).text().replaceAll("[编辑]", "[编辑]");
        //截取h2的内容成需要的字符串
        substring = text.substring(0, text.indexOf('['));
        for (int j = 1; j < trElements.size(); j++) {
            // TODO academicianTitle，electionYear，titleYears，workCompany，relatedLinks
            Elements tdElemnets = trElements.get(j).select("td");
            PersonInfo personInfo = new PersonInfo();
            //获取头衔
            personInfo.setAcademicianTitle(10);
            personInfo.setAcademicianTitleValue(AcademicianEnum.getEnum(10).getValue());
            //当选年
            personInfo.setElectionYear(map.get(substring));
            //获取年限
            personInfo.setTitleYears(3);
            //姓名
            personInfo.setName(tdElemnets.get(1).text());
            if (StringUtils.isNotBlank(tdElemnets.get(1).select("small").text())) {
                personInfo.setForeignName(tdElemnets.get(1).select("small").text());
            }
            personInfo.setMajor(tdElemnets.get(2).text());
            String company = tdElemnets.get(0).text();
            //获取工作单位
            personInfo.setWorkCompany(company);
            list.add(personInfo);

        }
        return list;
    }

    private PersonInfo parsePersonData(String personUrl, PersonInfo personInfo) {
        Document doc = CrawlerCommonUtils.getDocument(personUrl);
        String describe = doc.select(".mw-parser-output p").text().replaceAll("，", ",")
                .replaceAll("（", "(").replaceAll("）", ")").replaceAll("－", "-");
        String describeData = RegexUtils.replaceData(describe, RegexUtils.ALL_ZHONG_REGEX, "");
        String[] split = describeData.split(",");
        System.out.println(split[0]);
//        System.out.println(split[0].indexOf("("));
//        System.out.println(split[0].indexOf("-"));
        if (StringUtils.contains(split[0], "(")) {
            String birthday = split[0].substring(split[0].indexOf("(") + 1, split[0].indexOf("-"));
            //生日
            personInfo.setBirthday(birthday);
            if (split[0].length() > 18) {
                String dateOfDeath = split[0].substring(split[0].indexOf("-") + 1, split[0].indexOf(")"));
                //逝世日期
                personInfo.setDateOfDeath(dateOfDeath);
            }
        }
        if (describeData.contains(",男,")) {
            //性别
            personInfo.setSex("男");
        } else if (describeData.contains(",女,")) {
            personInfo.setSex("女");
        }
        if (split.length > 2) {
            if (split[2].contains("人")) {
                //出生地
                personInfo.setBirthplace(split[2].substring(0, split[2].indexOf("人")));
            }
        }
        //个人简介
        personInfo.setDescribeData(describeData);
//        System.out.println(describeData);
        return personInfo;
    }

    public static void updateScholar2011(List<PersonInfo> list, String year) throws IOException {
        Document doc = CrawlerCommonUtils.getDocumentHtml("/htmlTest/cj-长江学者/" + year + "-长江学者.html");
        Elements tableElements = doc.select("table");
//        System.out.println(tableElements.size());
        List<Map<String, String>> mapList = new ArrayList();
        Integer size = tableElements.size() - 1;
        for (int i = 0; i < size; i++) {
            Elements tbodyTrs = tableElements.get(i).select("tbody tr");
            for (int j = 1; j < tbodyTrs.size(); j++) {

                try {
                    Map<String, String> trMap = new HashMap<>();
                    // 聘任学校
                    trMap.put("appointmentSchool", tbodyTrs.get(j).select("td").get(0).text().replaceAll("（", "(").replaceAll("）", ")"));
                    // 学科/专业
                    trMap.put("major", tbodyTrs.get(j).select("td").get(1).text());
                    // 姓名 Pol D.Spanos  刘坚能(Vincent LAU)
                    if (StringUtils.equals(tbodyTrs.get(j).select("td").get(2).text(), "Pol D.Spanos")) {
                        trMap.put("name", "波尔·斯帕诺斯");
                    } else if (StringUtils.equals(tbodyTrs.get(j).select("td").get(2).text(), "刘坚能(Vincent LAU)")) {
                        trMap.put("foreignName", RegexUtils.getTextContent(tbodyTrs.get(j).select("td").get(2).text(), RegexUtils.ALL_KUA_REGEX_TWO));
                        trMap.put("name", RegexUtils.replaceData(tbodyTrs.get(j).select("td").get(2).text(), RegexUtils.ALL_KUA_REGEX, ""));
                    } else {
                        trMap.put("name", tbodyTrs.get(j).select("td").get(2).text());
                    }
                    // 国籍
                    trMap.put("nationality", tbodyTrs.get(j).select("td").get(3).text());
                    // 工作单位
                    trMap.put("workCompany", RegexUtils.replaceData(tbodyTrs.get(j).select("td").get(4).text(), RegexUtils.ALL_ZHONG_REGEX, "").replaceAll("（", "(").replaceAll("）", ")"));
                    mapList.add(trMap);
                } catch (Exception e) {
                    log.error("{}年,name：{}，申报学校：{}，学科/专业：{}，国籍：{}",
                            2011, tbodyTrs.get(j).select("td").get(2).text(), tbodyTrs.get(j).select("td").get(0).text(),
                            tbodyTrs.get(j).select("td").get(1).text(), tbodyTrs.get(j).select("td").get(3).text());
                }
            }
        }

        Set<PersonInfo> addPersonInfoSet = getPersonInfos(list, mapList, year);
        if (CollectionUtils.isNotEmpty(addPersonInfoSet)) {
            for (PersonInfo personInfo : addPersonInfoSet) {
                list.add(personInfo);
            }
        }
//        log.info("2011-长江学者新增数量:{},2011-长江学者新增数据:{}", addPersonInfoList.size(), JSON.toJSONString(addPersonInfoList));
//        log.info(JSON.toJSONString(list));
        log.info("2011-长江学者新增数量:{}，2011-长江学者新增数据：{}", addPersonInfoSet.size(), JSON.toJSONString(addPersonInfoSet));
    }

    private static Set<PersonInfo> getPersonInfos(List<PersonInfo> list, List<Map<String, String>> mapList, String year) {
//        List<PersonInfo> addPersonInfoList = new ArrayList<>();
        Set<PersonInfo> set = new HashSet<>();
        List<String> nameList = list.stream().map(PersonInfo::getName).collect(Collectors.toList());
        for (Map<String, String> map : mapList) {
            for (PersonInfo personInfo : list) {
                if (null != personInfo.getStamp() && 1 == personInfo.getStamp())
                    continue;
                String mapName = map.get("name").replaceAll("@", " ")
                        .replaceAll("（", "(").replaceAll("）", ")");
                String name = "";
                if (StringUtils.isNotBlank(personInfo.getName()))
                    name = personInfo.getName().replaceAll("@", " ");
                String foreignName = "";
                if (StringUtils.contains(mapName, "(")) {
                    foreignName = RegexUtils.getTextContent(mapName, RegexUtils.ALL_KUA_REGEX_TWO);
                    mapName = RegexUtils.replaceData(mapName, RegexUtils.ALL_KUA_REGEX, "");
                    foreignName = foreignName.replaceAll("@", " ")
                            .replaceAll("（", "(").replaceAll("）", ")");
                    map.put("foreignName", foreignName);
                    map.put("name", mapName);

                }
                if ((StringUtils.equals(name, mapName)
                        || StringUtils.isNotBlank(personInfo.getForeignName()) && StringUtils.equals(personInfo.getForeignName(), foreignName))
                        && StringUtils.equals(personInfo.getWorkCompany(), map.get("appointmentSchool"))
                        && StringUtils.equals(personInfo.getMajor(), map.get("major"))) {
                    personInfo.setNationality(map.get("nationality"));
                    if (map.containsKey("foreignName") && StringUtils.isNotBlank(map.get("foreignName"))) {
                        personInfo.setForeignName(map.get("foreignName"));
                    }
                    if (map.containsKey("personTitle") && StringUtils.isNotBlank(map.get("personTitle"))) {
                        personInfo.setPersonTitle(map.get("personTitle"));
                    }
                } else if ((StringUtils.equals(name, mapName)
                        || StringUtils.isNotBlank(personInfo.getForeignName()) && StringUtils.equals(personInfo.getForeignName(), foreignName))
                        && StringUtils.equals(personInfo.getWorkCompany(), map.get("appointmentSchool"))) {
                    personInfo.setNationality(map.get("nationality"));
                    if (map.containsKey("foreignName") && StringUtils.isNotBlank(map.get("foreignName"))) {
                        personInfo.setForeignName(map.get("foreignName"));
                    }
                    if (map.containsKey("personTitle") && StringUtils.isNotBlank(map.get("personTitle"))) {
                        personInfo.setPersonTitle(map.get("personTitle"));
                    }
                }

                if (StringUtils.equals(mapName, "Wang Zheng Bing")) {
                    PersonInfo personInfo1 = new PersonInfo().builder()
                            .workCompany(map.get("appointmentSchool"))
                            .nationality(map.get("nationality"))
                            .major(map.get("major"))
                            .academicianTitle(10)
                            .academicianTitleValue(AcademicianEnum.getEnum(10).getValue())
                            .titleYears(3)
                            .electionYear(year)
                            .build();
                    personInfo1.setNationality("荷兰");
                    personInfo1.setForeignName("Wang Zheng Bing");
                    personInfo1.setName("Wang Zheng Bing");
                    if (!set.contains(personInfo1))
                        set.add(personInfo1);
                }

                if (StringUtils.equals(mapName, "Francesco Stellacci")) {
                    PersonInfo personInfo1 = new PersonInfo().builder()
                            .workCompany(map.get("appointmentSchool"))
                            .nationality(map.get("nationality"))
                            .major(map.get("major"))
                            .academicianTitle(10)
                            .academicianTitleValue(AcademicianEnum.getEnum(10).getValue())
                            .titleYears(3)
                            .electionYear(year)
                            .build();
                    personInfo1.setNationality("意大利");
                    personInfo1.setForeignName("Francesco Stellacci");
                    personInfo1.setName("Francesco Stellacci");
                    if (!set.contains(personInfo1))
                        set.add(personInfo1);
                }

                if (StringUtils.equals(mapName, "赵 震")) {
                    PersonInfo personInfo1 = new PersonInfo().builder()
                            .name(map.get("name"))
                            .workCompany(map.get("appointmentSchool"))
                            .nationality(map.get("nationality"))
                            .major(map.get("major"))
                            .academicianTitle(8)
                            .academicianTitleValue(AcademicianEnum.getEnum(8).getValue())
                            .titleYears(5)
                            .electionYear(year)
                            .build();
                    personInfo1.setName("赵震");
                    personInfo1.setNationality("中国");
                    if (!set.contains(personInfo1))
                        set.add(personInfo1);
                }

                if (StringUtils.equals(mapName, "Chen Yang")) {
                    PersonInfo personInfo1 = new PersonInfo().builder()
                            .workCompany(map.get("appointmentSchool"))
                            .nationality(map.get("nationality"))
                            .major(map.get("major"))
                            .personTitle(map.get("personTitle"))
                            .academicianTitle(8)
                            .academicianTitleValue(AcademicianEnum.getEnum(8).getValue())
                            .titleYears(5)
                            .electionYear(year)
                            .build();
                    personInfo1.setForeignName("Chen Yang");
                    personInfo1.setName("Chen Yang");
                    if (!set.contains(personInfo1))
                        set.add(personInfo1);
                }

                if (!nameList.contains(mapName) && !StringUtils.equalsAny(mapName,
                        "夏伯嘉R.Hsia", "Michael Pinedo", "Christos Samakovlis", "Steven H. Low", "James A. Ohlson",
                        "Francesco Stellacci", "赵 震", "Wang Zheng Bing", "Chen Yang", "Teik C. Lim", "Yury Gogotsi", "Huang Peng")) {
                    PersonInfo personInfo1 = new PersonInfo().builder()
                            .name(map.get("name"))
                            .workCompany(map.get("appointmentSchool"))
                            .nationality(map.get("nationality"))
                            .major(map.get("major"))
                            .academicianTitle(10)
                            .academicianTitleValue(AcademicianEnum.getEnum(10).getValue())
                            .titleYears(3)
                            .electionYear(year)
                            .build();
                    if (map.containsKey("foreignName") && StringUtils.isNotBlank(map.get("foreignName"))) {
                        personInfo1.setForeignName(map.get("foreignName"));
                    }
                    if (map.containsKey("personTitle") && StringUtils.isNotBlank(map.get("personTitle"))) {
                        personInfo1.setPersonTitle(map.get("personTitle"));
                    }
                    if (StringUtils.equalsAny(mapName, "肖杰", "王学斌", "马於光", "佟家栋", "钱锋", "杨耕", "翁杰敏", "方家松")) {

                        personInfo1.setAcademicianTitle(8);
                        personInfo1.setAcademicianTitleValue(AcademicianEnum.getEnum(8).getValue());
                        personInfo1.setTitleYears(5);
                        personInfo1.setElectionYear(year);
                    }

//                    MD5Util.encrypt32(personInfo1);
//                    list.add(personInfo1);
//                    addPersonInfoList.add(personInfo1);
                    if (!set.contains(personInfo1))
                        set.add(personInfo1);

                }

                if (StringUtils.equals(personInfo.getName(), "刘坚能") && personInfo.getAcademicianTitle() == 10) {
                    personInfo.setNationality("中国");
                    personInfo.setForeignName("Vincent LAU");
                }

                if (StringUtils.equals(personInfo.getForeignName(), "Michael Pinedo") && personInfo.getAcademicianTitle() == 10) {
                    personInfo.setNationality("美国");
                    personInfo.setForeignName("Michael Pinedo");
                }

                if (StringUtils.equals(personInfo.getForeignName(), "Christos Samakovlis") && personInfo.getAcademicianTitle() == 10) {
                    personInfo.setNationality("瑞典");
                    personInfo.setForeignName("Christos Samakovlis");
                }

                if (StringUtils.equals(personInfo.getForeignName(), "Steven H. Low") && personInfo.getAcademicianTitle() == 10) {
                    personInfo.setNationality("美国");
                    personInfo.setForeignName("Steven H. Low");
                }

                if (StringUtils.equals(personInfo.getForeignName(), "James A. Ohlson") && personInfo.getAcademicianTitle() == 10) {
                    personInfo.setNationality("美国");
                    personInfo.setForeignName("James A. Ohlson");
                }

//                if (StringUtils.equals(personInfo.getName(), "Francesco Stellacci") && personInfo.getAcademicianTitle() == 10) {
//                    personInfo.setNationality("意大利");
//                    personInfo.setForeignName("Francesco Stellacci");
//                }

                if (StringUtils.equals(personInfo.getName(), "夏伯嘉") && StringUtils.equals(personInfo.getForeignName(), "R.Hsia")) {
                    personInfo.setNationality("美国");
                }
                if (StringUtils.equalsAny(personInfo.getName(), "Teik C. Lim", "Yury Gogotsi", "Huang Peng")) {
                    personInfo.setPersonTitle("教授");
                    personInfo.setNationality("美国");
                    personInfo.setForeignName(personInfo.getName());
                }
            }
        }
        return set;
    }


    public static void updateScholarByYear(List<PersonInfo> list, String year) throws IOException {
//        Document doc = CrawlerCommonUtils.getDocumentHtml("/htmlTest/cj-长江学者/2005-长江学者.txt");
        String url = "/htmlTest/cj-长江学者/" + year + "-长江学者.txt";
        Resource resource = new ClassPathResource(url);
        String filePath = resource.getFile().getAbsolutePath();
        File file = new File(filePath);
        List<String> lines = FileUtils.readLines(file, StandardCharsets.UTF_8);
        List<Map<String, String>> mapList = new ArrayList();
        for (int i = 0; i < lines.size(); i++) {
            if (StringUtils.isNotBlank(lines.get(i))) {
                String[] strLines = lines.get(i).split(" ");
                // 申报学校 设岗学科 姓名 国籍 现任职单位 现任专业技术职务
                if (StringUtils.containsAny(strLines[0], "申报学校", "讲座教授", "特聘教授")) {
                    continue;
                }
                try {
                    Map<String, String> trMap = new HashMap<>();
                    // 申报学校
                    trMap.put("appointmentSchool", strLines[0].replaceAll("（", "(").replaceAll("）", ")"));
                    // 学科/专业
                    trMap.put("major", strLines[1]);
                    // 姓名
                    trMap.put("name", strLines[2]);
                    // 国籍
                    trMap.put("nationality", strLines[3]);
                    // 现任单位
                    trMap.put("workCompany", RegexUtils.replaceData(strLines[4], RegexUtils.ALL_ZHONG_REGEX, "").replaceAll("（", "(").replaceAll("）", ")"));
                    // 现任专业技术职务
                    trMap.put("personTitle", strLines[5]);
                    mapList.add(trMap);
                } catch (Exception e) {
                    log.error("{}年,name：{}，申报学校：{}，学科/专业：{}，国籍：{}，现任单位：{}",
                            year, strLines[2], strLines[0], strLines[1], strLines[3], strLines[4]);
                }
            }
        }

        Set<PersonInfo> addPersonInfoSet = getPersonInfos(list, mapList, year);
        if (CollectionUtils.isNotEmpty(addPersonInfoSet)) {
            for (PersonInfo personInfo : addPersonInfoSet) {
                list.add(personInfo);
            }
        }

        log.info("{}年-长江学者新增数量:{},{}年-长江学者新增数据:{}", year, addPersonInfoSet.size(), year, JSON.toJSONString(addPersonInfoSet));
    }

    /**
     * 2016年长江学者    讲座教授和青年学者
     */
    public List<PersonInfo> crawlerData() throws IOException {
        Document doc = CrawlerCommonUtils.getDocumentHtml("/htmlTest/cj-长江学者/2016-长江学者.html");
        Elements tableEle = doc.select(".wenben table");
        List<PersonInfo> list = new ArrayList<>();
        for (int i = 1; i < tableEle.size(); i++) {
            Elements trElements = tableEle.get(i).select("tbody tr");
            for (int j = 1; j < trElements.size(); j++) {
                Elements tdElements = trElements.get(j).select("td");
                PersonInfo personInfo = new PersonInfo();
                //推荐学校
                personInfo.setWorkCompany(tdElements.get(0).text().replaceAll("（", "(").replaceAll("）", ")").replaceAll(" ", ""));
                personInfo.setName(tdElements.get(1).text());
                personInfo.setMajor(tdElements.get(2).text());
                if (i == 1) {
                    personInfo.setAcademicianTitle(10);
                    personInfo.setAcademicianTitleValue(AcademicianEnum.CHANGJIANG_SCHOLAR_LECTURE.getValue());
                } else {
                    personInfo.setAcademicianTitle(11);
                    personInfo.setAcademicianTitleValue(AcademicianEnum.CHANGJIANG_SCHOLAR_YOUTH.getValue());
                }
                list.add(personInfo);
                personInfo.setElectionYear("2016年");
                personInfo.setTitleYears(3);
            }
            System.out.println(JSON.toJSONString(list));
        }
        return list;
    }

    /**
     * 2017年长江学者    讲座教授和青年学者
     */
    public List<PersonInfo> crawlerData1() throws IOException {
        Document doc = CrawlerCommonUtils.getDocumentHtml("/htmlTest/cj-长江学者/2017-长江学者.html");
        Elements tableEle = doc.select("#ContentArea div table");
        List<PersonInfo> list = new ArrayList<>();
        for (int i = 1; i < tableEle.size(); i++) {
            Elements trElements = tableEle.get(i).select("tbody tr");
            for (int j = 1; j < trElements.size(); j++) {
                Elements tdElements = trElements.get(j).select("td");
                PersonInfo personInfo = new PersonInfo();
                //推荐学校
                personInfo.setWorkCompany(tdElements.get(0).text().replaceAll("（", "(").replaceAll("）", ")").replaceAll(" ", ""));
                personInfo.setName(tdElements.get(1).text());
                personInfo.setMajor(tdElements.get(2).text());
                // 现任职单位
                tdElements.get(3).text().replaceAll("（", "(").replaceAll("）", ")").replaceAll(" ", "");
                if (i == 1) {
                    personInfo.setAcademicianTitle(10);
                    personInfo.setAcademicianTitleValue(AcademicianEnum.CHANGJIANG_SCHOLAR_LECTURE.getValue());
                } else {
                    personInfo.setAcademicianTitle(11);
                    personInfo.setAcademicianTitleValue(AcademicianEnum.CHANGJIANG_SCHOLAR_YOUTH.getValue());
                }
                personInfo.setElectionYear("2017年");
                personInfo.setTitleYears(3);
                list.add(personInfo);
            }
            System.out.println(JSON.toJSONString(list));
        }
        return list;
    }

    /**
     * 添加2015年长江学者的青年学者
     *
     * @return
     * @throws IOException
     */
    public List<PersonInfo> parseData() throws IOException {
        String url = "/htmlTest/cj-长江学者/2015年青年学者.txt";
        Resource resource = new ClassPathResource(url);
        String filePath = resource.getFile().getAbsolutePath();
        File file = new File(filePath);
        List<String> lines = FileUtils.readLines(file, StandardCharsets.UTF_8);
        List<PersonInfo> list = new ArrayList<>();
        for (int i = 1; i < lines.size() - 4; i += 4) {

            PersonInfo personInfo = new PersonInfo();
            for (int j = i; j < i + 4; j++) {
                if (j % 4 == 2) {
                    personInfo.setName(lines.get(j));
                }
                if (j % 4 == 3) {
                    personInfo.setMajor(lines.get(j).replaceAll("（", "(").replaceAll("）", ")"));
                }
                if (j % 4 == 0) {
                    personInfo.setWorkCompany(lines.get(j).replaceAll("（", "(").replaceAll("）", ")").replaceAll("\\.", ""));
                }
            }
            personInfo.setElectionYear("2015年");
            personInfo.setAcademicianTitle(11);
            personInfo.setAcademicianTitleValue("青年长江学者-青年学者");
            personInfo.setTitleYears(3);
            list.add(personInfo);
        }
        return list;
    }

    /**
     * 长江学者补充数据
     *
     * @return
     * @throws IOException
     */
    public List<PersonInfo> parseScholarExcel(String filePath) {
//        org.springframework.core.io.Resource resource = new ClassPathResource("/htmlTest/cj-长江学者/cj-补充数据1.xlsx");
//        String filePath = resource.getFile().getAbsolutePath();
        Long id = null;
        List<PersonInfo> list = new ArrayList<>();
        try {
            XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(new File(filePath)));
            //根据页面index 获取sheet页
            XSSFSheet sheet = wb.getSheetAt(0);        //实体类集合
            //循环sesheet页中数据从第二行开始，第一行是标题
            int rows = sheet.getPhysicalNumberOfRows();

            // 已导入的青长，过滤掉
            String ids = "3025,1042,2884,6169,6193,6142,6144,6124,6097,6158,5801,6115,6121,6180,6537,5800,6191,6550,6186,6137,6549,6159,6500,6086,6092,5817,6538,6512,6194,6502,6213,6143,6123,6184,6201,6095,6084,6157,6168,6133,6530,6091,5808,6173,6167,6093,6518,6100,6153,6532,6113,6107,6203,6192,6197,6106,6212,6546,6183,6147,5810,6198,6089,6508,5796,6126,6108,6151,5797,5809,5813,5805,6211,6501,6490,6520,6516,6188,6162,6511,6189,6528,6208,6112,6206,6513,6166,5811,5807,6540,6531,6170,6171,6085,5803,6517,6181,6102,6524,6135,5799,6499,6535,6529,6545,5802,6504,6139,5820,5819,6149,6141,5804,6527,6492,6515,6160,6161,6164,6132,6506,5822,6101,6178,6128,6536,6136,6493,6163,6202,6488,6495,6510,6491,6088,6174,6129,6185,6172,6127,6096,6176,6543,6148,6533,6099,6547,6210,6138,6122,5815,6090,6109,6551,6195,6131,6497,6145,6542,6534,6548,6209,6505,6494,6117,6150,6521,6154,6094,6498,5806,6519,6514,6182,5798,6539,6125,6118,6526,6509,6199,6205,5816,6155,6140,6110,6087,6522,6200,6496,6525,6130,6503,6134,6489,5818,5812,6507,6207,6098,6103,6523,6544";
            String[] idArr = ids.split(",");
            List<Long> idList = new ArrayList<>();
            for (String id2 : idArr) {
                idList.add(Long.parseLong(id2));
            }

            for (int i = 1; i < rows; i++) {
                XSSFRow row = sheet.getRow(i);
                PersonInfo personInfo = new PersonInfo();
                if (row.getCell(0) == null)
                    continue;
                double numericCellValue = row.getCell(0).getNumericCellValue();
                id = new Double(numericCellValue).longValue();
                if (idList.contains(id)) {
                    continue;
                }

                personInfo.setId(id);
                personInfo.setName(row.getCell(1).getStringCellValue());
                if (row.getCell(2) != null && StringUtils.isNotBlank(row.getCell(2).getStringCellValue()))
                    personInfo.setForeignName(row.getCell(2).getStringCellValue());
                if (row.getCell(3) != null && StringUtils.isNotBlank(row.getCell(3).getStringCellValue())) {
                    if (StringUtils.containsAny(row.getCell(3).getStringCellValue(), "http://", "https://")) {
                        String imgPath = row.getCell(3).getStringCellValue();
                        try {
                            CrawlerCommonUtils.getPersonalImgSave(imgPath, "C:\\Users\\user\\Pictures\\长江学者\\img-长江学者\\", id + ".jpg");
                            personInfo.setImgName(id + ".jpg");
                        } catch (Exception e) {
                        }
                    } else {
                        personInfo.setImgName(row.getCell(3).getStringCellValue());
                    }
                }
                if (row.getCell(4) != null && StringUtils.isNotBlank(row.getCell(4).getStringCellValue()))
                    personInfo.setPersonTitle(row.getCell(4).getStringCellValue());
                String title = row.getCell(5).getStringCellValue();
                if (StringUtils.isNotBlank(title)) {
                    personInfo.setAcademicianTitle(AcademicianEnum.getEnum(title).getCode());
                    personInfo.setAcademicianTitleValue(AcademicianEnum.getEnum(title).getValue());
                    personInfo.setTitleYears(AcademicianEnum.getEnum(title).getTitleYears());
                }
                if (row.getCell(6) != null && StringUtils.isNotBlank(row.getCell(6).getStringCellValue()))
                    personInfo.setAcademicDegree(row.getCell(6).getStringCellValue());
                if (row.getCell(7) != null && StringUtils.isNotBlank(row.getCell(7).getStringCellValue()))
                    personInfo.setNationality(row.getCell(7).getStringCellValue());
                if (row.getCell(8) != null && StringUtils.isNotBlank(row.getCell(8).getStringCellValue()))
                    personInfo.setNation(row.getCell(8).getStringCellValue());
                System.out.println(row.getCell(9));
                if (row.getCell(9) != null && StringUtils.isNotBlank(row.getCell(9).toString())) {
                    CellType birthdayCellTypeEnum = row.getCell(9).getCellTypeEnum();
                    if (birthdayCellTypeEnum.equals(CellType.STRING)) {
                        try {
                            String ateOfDeath = row.getCell(9).getStringCellValue();
                            ateOfDeath = DateUtils.handleDate(ateOfDeath);
                            personInfo.setBirthday(ateOfDeath);
                        } catch (Exception e) {
                            personInfo.setBirthday(row.getCell(9).getStringCellValue());
                        }
                    } else if (birthdayCellTypeEnum.equals(CellType.NUMERIC)) {
                        Date date = HSSFDateUtil.getJavaDate(row.getCell(9).getNumericCellValue());
                        personInfo.setBirthday(DateUtils.dateToString(date, "yyyy-MM-dd"));
                    } else {
                        personInfo.setBirthday(row.getCell(9).toString());
                    }
                }

                if (row.getCell(10) != null && StringUtils.isNotBlank(row.getCell(10).toString())) {
                    CellType birthdayCellTypeEnum = row.getCell(10).getCellTypeEnum();
                    if (birthdayCellTypeEnum.equals(CellType.STRING)) {
                        try {
                            String ateOfDeath = row.getCell(10).getStringCellValue();
                            ateOfDeath = DateUtils.handleDate(ateOfDeath);
                            personInfo.setDateOfDeath(ateOfDeath);
                        } catch (Exception e) {
                            personInfo.setDateOfDeath(row.getCell(10).getStringCellValue());
                        }
                    } else if (birthdayCellTypeEnum.equals(CellType.NUMERIC)) {
                        System.out.println(row.getCell(10).getNumericCellValue());
                        Date date = HSSFDateUtil.getJavaDate(row.getCell(10).getNumericCellValue());
                        personInfo.setDateOfDeath(DateUtils.dateToString(date, "yyyy-MM-dd"));
                    } else {
                        personInfo.setDateOfDeath(row.getCell(10).toString());
                    }
                }

                if (row.getCell(11) != null && StringUtils.isNotBlank(row.getCell(11).getStringCellValue()))
                    personInfo.setBirthplace(row.getCell(11).getStringCellValue());
                if (row.getCell(12) != null && StringUtils.isNotBlank(row.getCell(12).getStringCellValue()))
                    personInfo.setSex(row.getCell(12).getStringCellValue());
                if (row.getCell(13) != null && StringUtils.isNotBlank(row.getCell(13).getStringCellValue()))
                    personInfo.setSchool(row.getCell(13).getStringCellValue());
                if (row.getCell(14) != null && StringUtils.isNotBlank(row.getCell(14).getStringCellValue()))
                    personInfo.setDepartment(row.getCell(14).getStringCellValue());
                if (row.getCell(15) != null && StringUtils.isNotBlank(row.getCell(15).getStringCellValue()))
                    personInfo.setMajor(row.getCell(15).getStringCellValue());


                if (row.getCell(16) != null && StringUtils.isNotBlank(row.getCell(16).getStringCellValue()))
                    personInfo.setWorkCompany(row.getCell(16).getStringCellValue());
                if (row.getCell(17) != null && StringUtils.isNotBlank(row.getCell(17).getStringCellValue()))
                    personInfo.setCompanyAddress(row.getCell(17).getStringCellValue());


                if (row.getCell(18) != null && StringUtils.isNotBlank(row.getCell(18).getStringCellValue()))
                    personInfo.setCompanyType(row.getCell(18).getStringCellValue());

                if (row.getCell(19) != null && StringUtils.isNotBlank(row.getCell(19).getStringCellValue()))
                    personInfo.setPoliticalParty(row.getCell(19).getStringCellValue());

                if (row.getCell(20) != null && StringUtils.isNotBlank(row.getCell(20).getStringCellValue()))
                    personInfo.setDescribeData(row.getCell(20).getStringCellValue());

                if (row.getCell(21) != null && StringUtils.isNotBlank(row.getCell(21).getStringCellValue()))
                    personInfo.setResume(row.getCell(21).getStringCellValue());

                if (row.getCell(22) != null && StringUtils.isNotBlank(row.getCell(22).getStringCellValue()))
                    personInfo.setSocialService(row.getCell(22).getStringCellValue());

                if (row.getCell(23) != null && StringUtils.isNotBlank(row.getCell(23).getStringCellValue()))
                    personInfo.setResearchContents(row.getCell(23).getStringCellValue());

                if (row.getCell(24) != null && StringUtils.isNotBlank(row.getCell(24).getStringCellValue()))
                    personInfo.setAchievement(row.getCell(24).getStringCellValue());

                if (row.getCell(25) != null && StringUtils.isNotBlank(row.getCell(25).getStringCellValue()))
                    personInfo.setScientificResearchAwards(row.getCell(25).getStringCellValue());

                if (row.getCell(26) != null && StringUtils.isNotBlank(row.getCell(26).getStringCellValue()))
                    personInfo.setHonor(row.getCell(26).getStringCellValue());

//            CellType cellTypeEnum = row.getCell(28).getCellTypeEnum();
//            if (cellTypeEnum.equals(CellType.STRING)) {
//                personInfo.setElectionYear(row.getCell(28).getStringCellValue());
//            } else {
//                double num1 = row.getCell(28).getNumericCellValue();
//                String electionYear = Double.toString(num1);
//                personInfo.setElectionYear(electionYear);
//            }
//            double num = row.getCell(29).getNumericCellValue();
//            int titleYears = new Double(num).intValue();
//            personInfo.setTitleYears(titleYears);
                list.add(personInfo);
            }
        } catch (Exception e) {
            log.error("解析Excel模版异常，id：{} Exception：{}", id, e);
            return null;
        }
        return list;
    }
}
